package com.bjpowernode.crm.setting.web.controller;

import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.setting.service.UserService;
import com.bjpowernode.crm.setting.vo.LoginMessage;
import com.bjpowernode.crm.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService service;
    @ResponseBody
    @RequestMapping(value ="/setting/user/login.do" )
    public LoginMessage login(HttpServletRequest request, String loginAct, String loginPwd){
        //获取浏览器的ip地址
        String ip=request.getRemoteAddr();
        System.out.println(ip);
        //将密码转化为加密形式
        loginPwd=MD5Util.getMD5(loginPwd);
        User user=new User();
        user.setLoginAct(loginAct);
        user.setLoginPwd(loginPwd);
        user.setAllowIps(ip);
        LoginMessage login=new LoginMessage();
        try{
            User loginUser=service.login(user);
                //登陆成功
            request.getSession().setAttribute("loginUser",loginUser);
            //登录成功表示给前端返回一个success为true的信息，表示登陆成功
            login.setSuccess(true);
        }catch (Exception ex){
            ex.printStackTrace();
            //表示登录失败
            login.setMsg(ex.getMessage());
        }finally {
            return login;
        }

    }
    //查询所有用户信息
    @ResponseBody
    @RequestMapping(value = "/setting/user/selectUsers.do")
    public List<User> selectUsers(){
        List<User> userList=service.selectUsers();
        return userList;
    }
}
